import wepy from 'wepy'
export default class extends wepy.mixin {
  data = {
    cid: '',
    query: '',
    pagenum: 1,
    pagesize: 10,
    goodsList: [],
    total: 0,
    isOver: false,
    isLoading: false
  }
  onLoad(options) {
    console.log(options)
    this.cid = options.cid || ''
    this.query = options.query || ''
    this.getGoodsList()
  }
  methods = {
    goGoodsDetail(id) {
      wepy.navigateTo({
        url: `/pages/goods_detail/main?goods_id=${id}`
      })
    }
  }
  async getGoodsList(cb) {
    this.isLoading = true
    const {data: res} = await wepy.getData('/goods/search', {
      query: this.query,
      cid: this.cid,
      pagenum: this.pagenum,
      pagesize: this.pagesize
    })
    console.log(res)
    this.goodsList = [...this.goodsList, ...res.message.goods]
    this.total = res.message.total
    this.isLoading = false
    this.$apply()
    cb && cb()
  }
  onReachBottom() {
    if (this.isLoading) {
      return
    }
    if (this.pagenum * this.pagesize >= this.total) {
      this.isOver = true
      return
    }
    console.log('chudile')
    this.pagenum++
    this.getGoodsList()
  }
  onPullDownRefresh() {
    this.pagenum = 1
    this.total = 0
    this.goodsList = []
    this.isLoading = this.isOver = false
    this.getGoodsList(() => {
      wepy.stopPullDownRefresh()
    })
  }
}
